package com.winona.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.R;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.domain.UserInfo;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.core.util.json.JsonUtil;
import com.winona.Base1Activity;
import com.winona.ConstantValue;
import com.winona.bean.Paper;
import com.winona.widget.PullToRefreshView;

/**
 * 学术论文
 * 
 * @author djbian
 * 
 */
public class PapersActivity extends Base1Activity {
	protected static final String TAG = "PapersActivity";
	private int PAGE_SIZE = 10;
	private SharedPreferences sp;
	private TextView title;
	private ImageView back;
	private Intent intent;
	private PullToRefreshView mRefreshView;
	private ListView listView;
	private BaseAdapter mAdapter;

	private boolean isClear = false;

	private int gotopage = 1;
	private Paper paper;

	private List<Paper> paperList = null;
	private boolean hasMore = true;

	private UserInfo user = AppConfigFactory.getInstance().getUserInfo();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		setContentView(R.layout.inport_news);
		findView();

		setLinstener();
		init();
		getData();

	}

	private void init() {
		title.setText("学术论文");
		paperList = new ArrayList<Paper>();
		mRefreshView.disableScroolDown();
		mRefreshView.enableScroolUp();
		mRefreshView
				.setOnFooterRefreshListener(new PullToRefreshView.OnFooterRefreshListener() {

					@Override
					public void onFooterRefresh(PullToRefreshView view) {
						getData();
					}
				});
	}

	private void getData() {
		if (!hasMore) {
			Toast.makeText(getApplicationContext(), "没有了", Toast.LENGTH_SHORT)
					.show();
			mRefreshView.onFooterRefreshComplete();
			return;
		}

		new SimpleBaseCommand() {

			@Override
			public Result go() {
				int gotopage = 1;
				if (paperList != null && paperList.size() > PAGE_SIZE) {
					gotopage = paperList.size() / PAGE_SIZE;
					if (paperList.size() % PAGE_SIZE > 0) {
						gotopage += 1;
					}
				}
				return new HttpTemplate().exchange(ConstantValue.COMMON
						+ "/academic/project/finds?academicid=1&gotopage="
						+ gotopage + "&pagesize=" + PAGE_SIZE
						+ "&role=EXPERT&userid=" + user.getUserid());

			}

			@Override
			public void no(Result result) {
				mRefreshView.onFooterRefreshComplete();
			}

			@Override
			public void yes(Result result) {
				mRefreshView.onFooterRefreshComplete();
				String json = result.object.toString();
				if (StringUtils.isNotEmpty(json)) {
					try {
						org.json.JSONObject jo = new org.json.JSONObject(json);
						String jstr = null;
						jstr = jo.getString("body");
						paperList = JsonUtil.jsonToList(jstr, Paper.class);

					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				if (mAdapter == null) {
					mAdapter = new PaperAdapter();
					listView.setAdapter(mAdapter);
				} else {
					mAdapter.notifyDataSetChanged();
				}
			}

		}.execute();

	}

	private void setLinstener() {
		// pull.onHeaderRefreshComplete();
		// pull.onFooterRefreshComplete();

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				startActivity(AcademicCenterActivity.class);
			}
		});

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("Paper_Thesis", paperList.get(position));

				startActivity(ThesisTitleActivity.class, map);

			}

		});

	}

	private void findView() {

		title = (TextView) findViewById(R.id.inport_title);
		back = (ImageView) findViewById(R.id.inport_back);
		listView = (ListView) findViewById(R.id.inport_list_view);
		mRefreshView = (PullToRefreshView) findViewById(R.id.pull_refresh_view);
		listView.setCacheColorHint(0);

	}

	private class PaperAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return paperList.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = View.inflate(getApplicationContext(),
					R.layout.sensivive_item, null);
			ImageView iv_logo = (ImageView) view
					.findViewById(R.id.sensitive_img);
			TextView tv_name = (TextView) view
					.findViewById(R.id.sensitive_name);
			TextView description = (TextView) view
					.findViewById(R.id.sensitive_description);
			View new_img = view.findViewById(R.id.new_img);

			if (position < 1) {
				new_img.setVisibility(View.VISIBLE);
			} else {
				new_img.setVisibility(View.GONE);
			}

			tv_name.setText(paperList.get(position).getAcademicProject()
					.getName());
			description.setText(paperList.get(position).getAcademicProject()
					.getContent());
			if (!TextUtils.isEmpty(paperList.get(position).getAcademicProject()
					.getImageUrl())) {
				ImageManager.getInstance().loadImage(
						ConstantValue.IMAGE
								+ paperList.get(position).getAcademicProject()
										.getImageUrl(), iv_logo, 60, 60, null);
			}

			return view;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

	}

}
